package com.budayi.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class RandomStrUtil {

    public static String getRandAllStr(int num){
        String str="";
        Random random = new Random();
        for(int i = 0; i < num; i++) {
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                //输出是大写字母还是小写字母
                str += (char)(random.nextInt(26) + 97);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                str += String.valueOf(random.nextInt(10));
            }
        }
        return str;
    }

    public static String getRandStr(int num){
        String str="";
        Random random = new Random();
        for(int i = 0; i < num; i++) {
           str += (char)(random.nextInt(26) + 97);
        }
        return str;
    }

    public static String getRandInt(int num){
        String str="";
        Random random = new Random();
        for(int i = 0; i < num; i++) {
            str += String.valueOf(random.nextInt(10));
        }
        return str;
    }

    public static List getRandomList(List paramList, int count){
        if(paramList.size()<count){
            return paramList;
        }
        Random random=new Random();
        List<Integer> tempList=new ArrayList<>();
        List<Object> newList=new ArrayList<Object>();
        int temp=0;
        for(int i=0;i<count;i++){
            temp=random.nextInt(paramList.size());//将产生的随机数作为被抽list的索引
            if(!tempList.contains(temp)){
                tempList.add(temp);
                newList.add(paramList.get(temp));
            }
            else{
                i--;
            }
        }
        return newList;
    }

    public static void main(String[] args) {
//        System.out.println(RandomStrUtil.getRandAllStr(6));
//        System.out.println(RandomStrUtil.getRandStr(6));
        System.out.println(RandomStrUtil.getRandInt(20));
    }
}
